
library(xtable)
library(matchMulti)
library(dplyr)
library(ggplot2)
library(nlme)

rm(list=ls())

setwd("~/Dropbox/Group Matches/Analysis/Catholic/ReplicationFiles/R output")
load("cschools_matchout.RData")


names <- c("Minority Student", "Female", "Student SES", "% Students Minority", "% Students Female",
			"Enrollment", "% Students on Academic Track", "Disciplinary Climate Scale", "School SES Average")

table_1 <- bal_unmatched[,1:3]
rownames(table_1) <- names
table_1
xtable(table_1)

# Covariate Lists
student.cov <- c('minority','female','ses')
school.cov <- c('minority_mean', 'female_mean', 'size', 'acad', 'discrm', 'ses_mean')
all.cov <- c('minority','female','ses','minority_mean', 'female_mean', 'size', 'acad', 'discrm', 'ses_mean')

# Balance After Matching
bal.match1 <- balanceMulti(match_1, student.cov = student.cov, school.cov)
bal.match2 <- balanceMulti(match_2, student.cov = student.cov, school.cov)
bal.match3 <- balanceMulti(match_3, student.cov = student.cov, school.cov)
bal.match4 <- balanceMulti(match_4, student.cov = student.cov, school.cov)
bal.match5 <- balanceMulti(match_5, student.cov = student.cov, school.cov)


school_names <- c("% Students Minority", "% Students Female",
			"Enrollment", "% Students on Academic Track", "Disciplinary Climate Scale", "School SES Average")


table_2 <- cbind(bal.match1$schools[,3], bal.match1$schools[,8], bal.match2$schools[,8], bal.match3$schools[,8],
				 bal.match4$schools[,8], bal.match5$schools[,8])

rownames(table_2) <- school_names
table_2
xtable(table_2)


match.data1 <- as.data.frame(match_1$matched)
length(unique(match.data1$school[match.data1$sector==1]))

match.data2 <- as.data.frame(match_2$matched)
length(unique(match.data2$school[match.data1$sector==1]))

match.data3 <- as.data.frame(match_3$matched)
length(unique(match.data3$school[match.data1$sector==1]))

match.data4 <- as.data.frame(match_4$matched)
length(unique(match.data4$school[match.data1$sector==1]))

match.data5 <- as.data.frame(match_5$matched)
length(unique(match.data5$school[match.data1$sector==1]))



# Outcome Analysis
match.data1 <- as.data.frame(match_1$matched)
match.data1$std_math_score <- (match.data1$mathach - mean(match.data1$mathach, na.rm = TRUE))/
  sd(match.data1$mathach [match.data1$sector == 0], na.rm = TRUE)
   
match.data2 <- as.data.frame(match_2$matched)
match.data2$std_math_score <- (match.data2$mathach - mean(match.data2$mathach, na.rm = TRUE))/
  sd(match.data2$mathach [match.data2$sector == 0], na.rm = TRUE)
  
match.data3 <- as.data.frame(match_3$matched)
match.data3$std_math_score <- (match.data3$mathach - mean(match.data3$mathach, na.rm = TRUE))/
  sd(match.data3$mathach [match.data3$sector == 0], na.rm = TRUE)
  
match.data4 <- as.data.frame(match_4$matched)
match.data4$std_math_score <- (match.data4$mathach - mean(match.data4$mathach, na.rm = TRUE))/
  sd(match.data4$mathach [match.data4$sector == 0], na.rm = TRUE)
  
match.data5 <- as.data.frame(match_5$matched)
match.data5$std_math_score <- (match.data5$mathach - mean(match.data5$mathach, na.rm = TRUE))/
  sd(match.data5$mathach [match.data5$sector == 0], na.rm = TRUE)


out1 <- lme(std_math_score ~ sector, random = ~ 1 | pair.id/school, data=match.data1)
out2 <- lme(std_math_score ~ sector, random = ~ 1 | pair.id/school, data=match.data2)
out3 <- lme(std_math_score ~ sector, random = ~ 1 | pair.id/school, data=match.data3)
out4 <- lme(std_math_score ~ sector, random = ~ 1 | pair.id/school, data=match.data4)
out5 <- lme(std_math_score ~ sector, random = ~ 1 | pair.id/school, data=match.data5)


row.1 <- c(summary(out1)$coef$fixed[2], intervals(out1)$fixed[2,1], intervals(out1)$fixed[2,3], summary(out1)$tTable[,"p-value"][2])
row.2 <- c(summary(out2)$coef$fixed[2], intervals(out2)$fixed[2,1], intervals(out2)$fixed[2,3], summary(out2)$tTable[,"p-value"][2])
row.3 <- c(summary(out3)$coef$fixed[2], intervals(out3)$fixed[2,1], intervals(out3)$fixed[2,3], summary(out3)$tTable[,"p-value"][2])
row.4 <- c(summary(out4)$coef$fixed[2], intervals(out4)$fixed[2,1], intervals(out4)$fixed[2,3], summary(out4)$tTable[,"p-value"][2])
row.5 <- c(summary(out5)$coef$fixed[2], intervals(out5)$fixed[2,1], intervals(out5)$fixed[2,3], summary(out5)$tTable[,"p-value"][2])

setwd("~/Dropbox/Group Matches/Analysis/Catholic/ReplicationFiles/R scripts")
source("print.table.R")
print.table(row.1, row.2, row.3, row.4, row.5)


out6 <- lme(std_math_score ~ sector + minority_mean + female_mean + size + acad + discrm + ses_mean, random = ~ 1 | pair.id/school, data=match.data1)
out7 <- lme(std_math_score ~ sector + minority_mean + female_mean + size + acad + discrm + ses_mean, random = ~ 1 | school, data=match.data2)
out8 <- lme(std_math_score ~ sector + minority_mean + female_mean + size + acad + discrm + ses_mean, random = ~ 1 | school, data=match.data3)
out9 <- lme(std_math_score ~ sector + minority_mean + female_mean + size + acad + discrm + ses_mean, random = ~ 1 | school, data=match.data4)
out10 <- lme(std_math_score ~ sector + minority_mean + female_mean + size + acad + discrm + ses_mean, random = ~ 1 | school, data=match.data5)

row.6 <- c(summary(out6)$coef$fixed[2], intervals(out6)$fixed[2,1], intervals(out6)$fixed[2,3], summary(out6)$tTable[,"p-value"][2])
row.7 <- c(summary(out7)$coef$fixed[2], intervals(out7)$fixed[2,1], intervals(out7)$fixed[2,3], summary(out7)$tTable[,"p-value"][2])
row.8 <- c(summary(out8)$coef$fixed[2], intervals(out8)$fixed[2,1], intervals(out8)$fixed[2,3], summary(out8)$tTable[,"p-value"][2])
row.9 <- c(summary(out9)$coef$fixed[2], intervals(out9)$fixed[2,1], intervals(out9)$fixed[2,3], summary(out9)$tTable[,"p-value"][2])
row.10 <- c(summary(out10)$coef$fixed[2], intervals(out10)$fixed[2,1], intervals(out10)$fixed[2,3], summary(out10)$tTable[,"p-value"][2])

print.table(row.6, row.7, row.8, row.9, row.10)


